from .astNode import Function, ProtoType, Environment


class StandardFunction(Function):
    def __init__(self, func):
        super().__init__({'para': None, 'body': None}, None)
        self.func = func

    def __call__(self, *args, **kwargs):
        return self.func(*args)


def get_type_fuc(v: ProtoType):
    return v.get_ele('@type_name')


def print_fuc(*args):
    print(*args)


def read_int(*args):
    return Variable(int(input()), '$int$')


def read_float(*args):
    return Variable(float(input()), '$float$')


def stop_func(*args):
    Environment.pause_flag = True


def continue_func(*args):
    Environment.pause_flag = False


standard_function_dict = {
    'type_of': get_type_fuc,
    'print': print_fuc,
    'read_int': read_int,
    'read_float': read_float,
    'stop': stop_func,
    'continue': continue_func
}


def init(env: Environment):
    for key, item in standard_function_dict.items():
        env.define(key, StandardFunction(item))
